'use strict';

const db = uniCloud.database()
const dbCmd = db.command

exports.main = async (event, context) => {
	
	const {
		userId,
		authorId
	} = event
	
	const user = await db.collection('user').doc(userId).get()
	const author_likes_ids = user.data[0].author_likes_ids
	
	let option = null
	
	if(author_likes_ids.includes(authorId)) { // 如果包含就删除，反之增加
		option = dbCmd.pull(authorId)
	} else {
		option = dbCmd.addToSet(authorId)
	}
	
	await db.collection('user').doc(userId).update({
		author_likes_ids: option
	})
	
	//返回数据给客户端
	return {
		code: 200,
		msg: '更新成功'
	}
};
